package com.micai.springboot.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micai.springboot.dto.UserDto;
import com.micai.springboot.entity.UserEntity;
import com.micai.springboot.mapper.UserMapper;
import com.micai.springboot.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 * <p>
 * Author: 赵新国
 * Date: 2017/10/24 18:36
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<UserDto> listUsers() {
        // 从缓存中获取用户列表信息
        /*String key = "user_list_key" ;
        ValueOperations<String, List<UserEntity>> operations = redisTemplate.opsForValue();*/

        // 缓存存在
        /*boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            List<UserEntity> userEntities = operations.get(key);
            logger.info("UserServiceImpl.listUsers() : 从缓存中获取了用户列表信息 >> " + userEntities.toString());
            List<UserDto> userDtos = toDtos(userEntities);
            return userDtos;
        }*/

        // 从 DB 中获取用户列表信息
        EntityWrapper<UserEntity> entityWrapper = new EntityWrapper<UserEntity>();
        List<UserEntity> userEntities = userMapper.selectList(entityWrapper);
        if (userEntities == null) {
            return null;
        }

        // 插入缓存
        /*operations.set(key, userEntities, 1000, TimeUnit.SECONDS);
        logger.info("UserServiceImpl.listUsers() : 用户列表信息插入缓存 >> " + userEntities.toString());*/
        List<UserDto> userDtos = toDtos(userEntities);
        return userDtos;
    }

    @Override
    public Page<UserEntity> getUserByPage(int pageNum, int pageSize, String userName) {
        PageHelper.startPage(pageNum, pageSize);
        // 从缓存中获取用户列表信息
        /*String key = "user_list_key" ;
        ValueOperations<String, List<UserEntity>> operations = redisTemplate.opsForValue();*/

        // 缓存存在
        /*boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            List<UserEntity> userEntities = operations.get(key);
            logger.info("UserServiceImpl.listUsers() : 从缓存中获取了用户列表信息 >> " + userEntities.toString());
            List<UserDto> userDtos = toDtos(userEntities);
            return userDtos;
        }*/

        // 从 DB 中获取用户列表信息
        /*EntityWrapper<UserEntity> entityWrapper = new EntityWrapper<UserEntity>();
        List<UserEntity> userEntities = userMapper.selectList(entityWrapper);
        if (userEntities == null) {
            return null;
        }*/

        // 插入缓存
        /*operations.set(key, userEntities, 1000, TimeUnit.SECONDS);
        logger.info("UserServiceImpl.listUsers() : 用户列表信息插入缓存 >> " + userEntities.toString());*/
        /*List<UserDto> userDtos = toDtos(userEntities);*/
        Map<String, Object> columnMap = new HashMap<String, Object>(16);
        if (!StringUtils.isBlank(userName)) {
            columnMap.put("user_name", userName);
        }
        Page<UserEntity> userEntities = userMapper.findByPage(columnMap);
        if (userEntities == null) {
            return null;
        }
        return userEntities;
    }

    private List<UserDto> toDtos(List<UserEntity> userEntities) {
        List<UserDto> userDtos = new ArrayList<UserDto>();
        for (UserEntity userEntity : userEntities) {
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(userEntity, userDto);
            userDtos.add(userDto);
        }
        return userDtos;
    }

    @Override
    public void save(UserDto userDto) {
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(userDto, userEntity);
        userMapper.insert(userEntity);
    }

    @Override
    public void update(UserDto userDto) {
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(userDto, userEntity);
        userMapper.updateById(userEntity);
    }

    @Override
    public UserDto getUserById(Long id) {
        UserEntity userEntity = userMapper.selectById(id);
        if (userEntity == null) {
            return null;
        }
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(userEntity, userDto);
        return userDto;
    }

    @Override
    public void delete(UserDto userDto) {
        userMapper.deleteById(userDto.getId());
    }

}
